// layout/index.js
import common from "../../../utils/common.js";
const nearbyColor = '#ff593d';
const { wholeHouseSvgSrc, flatShareSvgSrc, shortRentSvgSrc, needHouseSvgSrc } = require('../../../utils/iconSvg')
// const indexModuleArr = [{ iconSrc: wholeHouseSvgSrc(nearbyColor), text: '整租', urlParam: '/pages/nuan/select2/select2?rentType=0' }, { iconSrc: flatShareSvgSrc(nearbyColor), text: '合租', urlParam: '/pages/nuan/select2/select2?rentType=1' }, { iconSrc: shortRentSvgSrc(nearbyColor), text: '短租', urlParam: '/pages/nuan/select2/select2?otherSelectedVal=7' }, { iconSrc: needHouseSvgSrc(nearbyColor), text: '求租', urlParam: '/pages/nuan/need-page/needPage' },
const indexModuleArr = [{ iconSrc: wholeHouseSvgSrc(nearbyColor), text: '整租', urlParam: '/pages/nuan/select/all-select?rentType=0&fromType=1' }, { iconSrc: flatShareSvgSrc(nearbyColor), text: '合租', urlParam: '/pages/nuan/select/all-select?rentType=1&fromType=1' }, { iconSrc: shortRentSvgSrc(nearbyColor), text: '短租', urlParam: '/pages/nuan/select/all-select?fromType=1&otherSelectedVal=7' }, { iconSrc: needHouseSvgSrc(nearbyColor), text: '求租', urlParam: '/pages/nuan/need-page/needPage' },
]
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const { voiceSvgSrc, maleSvgSrc, femaleSvgSrc } = require('../../../utils/iconSvg')
const defaultColor = '#A6A6A6'
// const defaultColor = 'rgba(153,153,153, 1)';
const activeColor = 'rgba(255, 127, 80)';

const food = [
    'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
    'https://img1.baidu.com/it/u=1948650034,2409824853&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675',
    'https://img2.baidu.com/it/u=3016274568,4110305242&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313',
    'https://img2.baidu.com/it/u=2364493189,83457107&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500'
]
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

        food,
        // 获取导航栏信息
        // 导航栏的高度 = 状态栏高度 + 胶囊高度 +（胶囊top - 状态栏高度）* 2 
        dhlTop: app.globalData.dhlTop,
        dhlHeight: app.globalData.dhlHeight,
        windowHeight: app.globalData.windowHeight,

        // 模块数组展示
        indexModuleArr: indexModuleArr,

        houseArr: [],
        defaultAvatarUrl: defaultAvatarUrl,
        svgSrc: '',
        voiceSvgSrc: voiceSvgSrc(defaultColor),
        maleSvgSrc: maleSvgSrc(defaultColor),
        femaleSvgSrc: femaleSvgSrc(defaultColor),

        size: 20,
        current: 1,
        pages: 1,

        // 收藏 和 发布数量
        collectCount: 0,
        publishCount: 0,

    },


    /**
     * 点击首页模块
     * @param {首页模块}} e 
     */
    clickModule(e) {
        var url = e.currentTarget.dataset.url
        console.info('url=' + url)
        wx.navigateTo({
            url: url,
            success: function (res) {
                console.info('打开info页面成功')
            },
            fail: function (e) {
                console.info('打开info页面失败，e=' + JSON.stringify(e))
            }
        })
    },

    /**
     * scroll-view 上拉触底 加载数据
     */
    loadTodayHouseInfoList() {
        setTimeout(() => {
            var tempCurrent = this.data.current
            console.info('tempCurrent=' + tempCurrent);
            if (tempCurrent > this.data.pages) {
                console.info('tempCurrent=' + tempCurrent + " 已超过总页数=" + this.data.pages);
                return;
            }
            wx.showLoading({
                title: '加载中...',
            })
            var tempArr = this.data.houseArr
            // 获取、构造数据
            // 请求后端 
            wx.request({
                url: 'http://' + app.globalData.ipStr + '/house-rent/getTodayHouseList',
                method: 'POST',
                header: {
                    'content-type': 'application/json;charset=UTF-8'
                },
                data: { size: this.data.size, current: tempCurrent },
                success: res => {
                    console.info('当天房屋返回信息=' + JSON.stringify(res.data.data))
                    if (res.statusCode == 200) {
                        this.setData({
                            current: tempCurrent + 1,
                            houseArr: tempArr.concat(res.data.data.records),
                        })
                        if (tempCurrent == 1) {
                            this.setData({ pages: res.data.data.pages })
                        }
                    }
                },
                fail: e => {
                    console.info('当天房屋异常返回信息=' + JSON.stringify(e))
                },
                complete: c => {
                    wx.hideLoading()
                }
            })
        }, 500)

    },

    /**
     * 点击 房屋信息 跳转到详情页
     * @param {今日房屋} e 
     */
    toHouseInfo: function (e) {
        var houseId = e.currentTarget.dataset.id
        common.navigateTo2('/pages/nuan/info/houseInfo?houseId=' + houseId)
    },

    /**
     * 点击 跳转到群页面
     * @param {邀请进群} e 
     */
    toQrcode: function (e) {
        common.navigateTo2('/pages/nuan/qrcode/qrcode')
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const { logoSvg } = require('../../../utils/logoSvg.js')
        const svgImg = logoSvg('#eee')
        this.setData({ svgSrc: svgImg })

        if (wx.getStorageSync("token")) {
            this.getHouseCount(wx.getStorageSync("token"));
        } else {
            app.login_1128_2().then(tokenStr => {
                this.getHouseCount(wx.getStorageSync("token"));
            })
        }

        // 加载今日数据
        this.loadTodayHouseInfoList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

        console.info('windowHeight=' + this.data.windowHeight)
        console.info('dhlHeight=' + this.data.dhlHeight)
        console.info('dhlTop=' + this.data.dhlTop)

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    /**
     * 查询收藏和发布数量
     */
    getHouseCount(token) {
        wx.request({
            url: 'http://' + app.globalData.ipStr + '/house-view-collect/getHouseCount',
            method: 'POST',
            header: {
                'Authorization': token,
                'content-type': 'application/json;charset=UTF-8'
            },
            data: {},
            success: res => {
                if (res.data.code == 200) {
                    this.setData({
                        collectCount: res.data.data.collectCount,
                        publishCount: res.data.data.publishCount
                    })
                }
            },
            fail: e => {
                console.info('查询收藏和发布数量异常返回信息=' + JSON.stringify(e))
            },
            complete: c => {
                wx.hideLoading()
            }
        })
    },

    /**
    * 
    * @param {跳转到筛选页面} e 
    */
    toPreSelectHouse(e) {
        // var url = '/pages/nuan/pre-select/preSelect?type=0'
        var url = '/pages/nuan/select/all-select?fromType=0'
        common.navigateTo2(url)
    },

    /**
    * 
    * @param {跳转到收藏页面} e 
    */
    toCollectHouse: function (e) {
        var url = '/pages/nuan/collect/collectHouse'
        common.navigateTo(e, url)
    },

    /**
    * 
    * @param {跳转到我的发布页面} e 
    */
    toMyHouse: function (e) {
        var url = '/pages/nuan/my-house/myHouse'
        common.navigateTo(e, url)
    },
})